var config = require('../../../utils/config.js');
var netutil = require('../../../utils/netutil.js');
var baseutil = require('../../../utils/baseutil.js');
let { WeToast } = require('../../../src/wetoast.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allData: null,
    DataDepartment: null,
    selectUserID: null,
    selectDataUserInfo: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var selectUserID = "";
    var selectDataUserInfo = app.globalData.tempData
      app.globalData.tempData=null
    if (selectDataUserInfo != null) {
      for (var i = 0; i < selectDataUserInfo.length; i++) {
        var userInfo = selectDataUserInfo[i]
        if (!baseutil.isContains(selectUserID, userInfo.UserID)) {
          if (selectUserID.length > 0)
            selectUserID += "-" + userInfo.UserID
          else
            selectUserID += + userInfo.UserID
        }
      }

    }
    this.setData({
      TUserInfo: app.globalData.TUserInfo,
      selectUserID: selectUserID,
      selectDataUserInfo: selectDataUserInfo,
    })
    baseutil.log("selectUserID=" + selectUserID);
    baseutil.log(selectDataUserInfo);


    this.getEnterpriseUserList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  personnel: function (e) {
    var position = parseInt(e.currentTarget.dataset.position)


    var allData = this.data.allData;
    var DataDepartment = allData.DataDepartment
    var tempDataDepartment = new Array();
    for (var i = 0; i < DataDepartment.length; i++) {
      if (DataDepartment[i].ParentID == this.data.DataDepartment[position].DID) {
        tempDataDepartment.push(DataDepartment[i])
      }
    }
    //含有子部门  继续选择部门
    if (tempDataDepartment.length != 0) {
      this.setData({ DataDepartment: tempDataDepartment })
      return;
    }

    var DataUserInfo = allData.DataUserInfo
    var tempDataUserInfo = new Array();
    //不含有子部门  继续审批人
    for (var i = 0; i < DataUserInfo.length; i++) {
      if (DataUserInfo[i].DID == DataDepartment[position].DID) {
        if (baseutil.isContains(this.data.selectUserID, DataUserInfo[i].UserID)) {
          DataUserInfo[i].isSelected = true
        } else {
          DataUserInfo[i].isSelected = false
        }

        tempDataUserInfo.push(DataUserInfo[i])
      }
    }
    baseutil.log(tempDataUserInfo)
    app.globalData.tempData = tempDataUserInfo
    app.globalData.tempData2 = this.data.selectDataUserInfo
    
    
    wx.navigateTo({
      url: '../../approver/personnel/personnel?selectUserID=' + this.data.selectUserID,
    })
  },
  getEnterpriseUserList: function (e) {

    var THIS = this
    var url = config.getBaseUrl("CommonApi", "QueryAddressBook");
    var Data = new Object();
    Data.SiteId = this.data.TUserInfo.SiteID
    Data.EID = this.data.TUserInfo.EID

    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          var Data = res.data.Data;
          var DataDepartment = Data.DataDepartment
          var tempDataDepartment = new Array();
          for (var i = 0; i < DataDepartment.length; i++) {
            if (DataDepartment[i].Depth == '0') {
              tempDataDepartment.push(DataDepartment[i])
            }
          }

          THIS.setData({
            allData: Data,
            DataDepartment: tempDataDepartment
          })

        } else {
          wx.showToast({
            title: res.data.ResponseMetaInfo.Reason,
            icon: 'success',
            duration: 1000
          })
        }
      },
      function (res) {

      })
  },
  updateData: function () {
    this.setData({
      allData: null,
      DataDepartment: null,
      selectUserID: null,
      selectDataUserInfo:null})
      baseutil.log("清空数据");
  },
  getSearchText(e){
    var searchText = e.detail.value;
    baseutil.log(searchText)
    this.setData({
      searchText: searchText
    })
  },
  searchApprover(){
   


    var DataUserInfo = this.data.allData.DataUserInfo;
    var tempDataUserInfo = new Array();
    // //不含有子部门  继续审批人
    for (var i = 0; i < DataUserInfo.length; i++) {
      if (baseutil.isContains(DataUserInfo[i].TrueName, this.data.searchText) ||
       baseutil.isContains(DataUserInfo[i].UserName, this.data.searchText)) {
                tempDataUserInfo.push(DataUserInfo[i])
      }
    }
    baseutil.log(tempDataUserInfo)
    app.globalData.tempData = tempDataUserInfo
    app.globalData.tempData2 = this.data.selectDataUserInfoselectDataUserInfo


      wx.navigateTo({
      url: '../../approver/personnel/personnel?selectUserID=' + this.data.selectUserID,
    })
  }
})